# Maintainer: Antonio Rojas <arojas@archlinux.org>

pkgname=sagemath-doc
pkgver=10.1
pkgrel=2
pkgdesc='HTML documentation for SageMath'
arch=(any)
url='http://www.sagemath.org'
license=(GPL)
depends=(python-pplpy mathjax)
makedepends=(sagemath python-pyzmq python-docutils python-jupyter_client python-pkgconfig cython
             python-jupyter-sphinx python-sphinx-furo python-sphinx-copybutton)
source=(https://github.com/sagemath/sage/archive/$pkgver/sagemath-$pkgver.tar.gz
        fix-build.patch
        https://github.com/sagemath/sage/commit/a3e6bb2e.patch)
sha256sums=('a658612b1b2376ddaf207cc8ed0ef458d4c2880c16e19139bedbe8baa42ad62f'
            'ff0ae9866b6e6b74f8b5c947eacd2de50ec2dfd9ab4ca8d07e69df5a35a7c617'
            '1c7cd3f12be9330ccb19d5a8bc4c2f916fc13199995decf38fb00b73acb599fb')
options=(!strip) # nothing to strip, save packaging time

prepare() {
  cd sage-$pkgver

# fix build with sphinx 7.2
  patch -p1 -i ../fix-build.patch
# correctly detect mathjax path
  patch -p1 -i ../a3e6bb2e.patch

  SAGE_ROOT="$PWD" PATH="$PWD/build/bin:$PATH" src/doc/bootstrap
  make -C src/doc doc-src

  rm -r src/sage # Use the installed sage, not the one from the source tree
}

build() {
  cd sage-$pkgver/src

  export SAGE_SRC="$PWD" \
         SAGE_ROOT="$PWD/.." \
         SAGE_DOC="$PWD"/doc \
         PYTHONPATH="$PWD" \
         SAGE_NUM_THREADS=10 \
         LANGUAGE=C
  python -m sage_docbuild --no-pdf-links --mathjax all html
}

package() {
  cd sage-$pkgver/src/doc
 
  mkdir -p "$pkgdir"/usr/share/doc/sage
  cp -r common "$pkgdir"/usr/share/doc/sage
  cp -r html "$pkgdir"/usr/share/doc/sage

# Replace duplicated files by symlinks (Gentoo)
  cd "$pkgdir"/usr/share/doc/sage
  mv html/en/_static{,.tmp}
  for _dir in `find -name _static` ; do
    rm -r $_dir
    ln -s /usr/share/doc/sage/html/en/_static $_dir
  done
  mv html/en/_static{.tmp,}
}
